package com.example.listdemo;


import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.network.HttpAgent;
import com.example.utils.LogUtil;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
	private Button mbtnArray;
	private Button mbtnSample;
	private HttpAgent mHttpAgent;
	
	private String live_data_url = "http://192.168.1.100:8000/livedata/channel";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mHttpAgent = new HttpAgent(this);
		initBtn();
		//getJson();
		//initNetWork();
		String jsonString = getJsonString(live_data_url);
		if(jsonString != null){
			getJson(jsonString);
		}
		mbtnArray.setOnClickListener(this);
		mbtnSample.setOnClickListener(this);
	}
	
	private String getJsonString(String url){
		String result[];
		result = mHttpAgent.getMessageByGet(null, url, "sessionId", 0, 0);
		if (result[0].equals("200")){
			return result[1];
		}
		else 
			return null;
	}
	
	private void initNetWork(){
//		String url = "http://update.funshion.com/update/check_android.php?v=1.1.1.1&id=&client=apad&os_ver=3.0";
		String url = "http://www.baidu.com";
		String result[];
		result = mHttpAgent.getMessageByGet(null, url, "sessionId", 0, 0);
		Log.e("MainActivity", "staue:"+result[0]);
	}
	private void initBtn(){
		mbtnArray = (Button)findViewById(R.id.btnArray);
		mbtnSample = (Button)findViewById(R.id.btnSample);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		
		switch (id) {
		case R.id.btnArray:
			Log.e("MainActivity", "ArrayAdapterActivity");
			Intent arraryIntent = new Intent(MainActivity.this, ArrayAdapterActivity.class);
			MainActivity.this.startActivity(arraryIntent);
			break;
		case R.id.btnSample:
			Log.e("MainActivity","SampleAdapter");
			Intent  sampleIntent = new Intent(MainActivity.this, SampleAdapterActivity.class);
			MainActivity.this.startActivity(sampleIntent);
			break;
		default:
			break;
		}
	}

	private String jsonString= "[{\"name\":\"cctv1\",\"url\":\"www.baidu.com\"},{\"name\":\"cctv2\",\"url\":\"www.sina.com\"}]";
	public void  getJson(String jsonString){
		
		JSONObject obj;
		JSONArray array ;
		try {
			array = new JSONArray(jsonString);
			for(int i = 0; i<array.length(); i++){
				obj =  array.getJSONObject(i);
				Log.e("json","name="+obj.getString("name"));
				Log.e("json", "url="+obj.getString("url"));
			}			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
}
